<?php
namespace App\Controller;

use app\BaseController;

class Handle extends BaseController
{
    public function __construct(\think\App $app)
    {
        $this->app = $app;
        $this->request = $this->app->request;

        $this->UserModel = new \app\common\model\User;
        $this->FilesModel = new \app\common\model\Files;
        $this->RankModel = new \app\common\model\Rank;
        $this->HandleModel = new \app\common\model\Handle;
        $this->ProjectModel = new \app\common\model\Project;
    }

    //获取操作记录列表,仅超级管理员可以
    public function list(){
        if($this->request->isGet()){
            $userid = $this->request->param('userid', '', 'trim');
            $handtype = $this->request->param('handtype', '', 'trim');
            $page = $this->request->param('page', 1, 'trim');
            $limit = $this->request->param('limit', 50, 'trim');
            $sort = $this->request->param('sort', 'time', 'trim');
            $by = $this->request->param('by','desc', 'trim');

            // 检查身份等级
            $rankid = $this->UserModel->where('id',$userid)->value('rankid');
            $weigh = $this->RankModel->where('id', $rankid)->value('weigh');

            if($weigh*1 <= 1){
                return json(error_ful('当前账号无权限'));
            }

            // 偏移量
            $offset = ($page - 1) * $limit;

            $where = [];

            if(!empty($handtype)){
                $where['handtype'] = $handtype; 
            }
            
            $count = $this->HandleModel
                ->with('user')
                ->where($where)
                ->order($sort, $by)
                ->limit($offset,$limit)
                ->count();
            $handleList = $this->HandleModel
                ->with('user')
                ->where($where)
                ->order($sort, $by)
                ->limit($offset,$limit)
                ->select();

            if($count <= 0){
                return json(error_ful('暂无数据'));
            }

            foreach ($handleList as $key => $value) {
                if($value['user']){
                    unset($value['user']['password']);
                unset($value['user']['salt']);
                }
                
            }
            $data = [
                'count' => $count,
                'handList' => $handleList
            ];
            return json(success_ful('操作日志',$data));
            
        }
    }

    //新增操作记录
    public function create(){
        if($this->request->isGet()){
            $userid = $this->request->param('userid', '', 'trim');
            $proid = $this->request->param('proid', '', 'trim');
            $fileid = $this->request->param('fileid', '', 'trim');
            $handtype = $this->request->param('handtype', '', 'trim');
            $fileInfo = $this->FilesModel->where('id', $fileid)->find();
            if(empty($fileInfo)){
                return json(error_ful('文件不存在'));
            }

            $userInfo = $this->UserModel->where(['id'=>$userid])->find();
            if(empty($userInfo)){
                return json(error_ful('账号不存在'));
            }

            $proInfo = $this->ProjectModel->where(['id'=>$proid,'deletetime'=>null])->find();
            if(empty($proInfo)){
                return json(error_ful('项目不存在'));
            }

            if(empty($handtype)){
                return json(error_ful('操作类型不存在'));
            }

            $data = [
                'userid' => $userid,
                'time' => time(),
                'proid' => $proid,
                'filename' => $fileInfo['filename'],
                'handtype' => $handtype,
            ];

            $status = $this->HandleModel->insert($data);
            if($status === false){
                return json(error_ful('操作记录失败'));
            }else{
                return json(success_ful('操作记录成功'));
            }
        }
    }

    //修改操作记录信息,仅超级管理员可以
    public function update(){
        if($this->request->isGet()){
            
        }
    }

    //删除操作记录,仅超级管理员可以
    public function delete(){
        if($this->request->isGet()){
            
        }
    }
}